/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * cadastro.java
 *
 * Created on 18/01/2010, 17:17:49
 */

package forms;

import classes.Estabelecimentos;
import classes.estabelecimento;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import rad.Java.Common.DB.Database;

/**
 *
 * @author rogerio
 */
public class frmRegisterEstabelecimento extends javax.swing.JFrame {


    private DefaultTableModel dtmEstab;
    private Estabelecimentos listEstab;
    private estabelecimento estab;

    private boolean status = true;
    /** Creates new form cadastro */
    public frmRegisterEstabelecimento() throws SQLException {
        initComponents();
        
        listEstab = new Estabelecimentos();
        estab     = new estabelecimento();
        btnDeletar.setEnabled(false);
        btnEditar.setEnabled(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jtpEstabelecimentos = new javax.swing.JTabbedPane();
        jpnPesquisa = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        dgvEstabelecimentos = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        txtPesquisa = new javax.swing.JTextField();
        rbtNome = new javax.swing.JRadioButton();
        rbdCnpj = new javax.swing.JRadioButton();
        jpnCadastro = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtEstabNome = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtEstabAtividade = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtEstabNFantasia = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtEstabCpfCnpj = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtEstabEndereco = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtEstabMunicipio = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtEstabCep = new javax.swing.JFormattedTextField();
        jLabel9 = new javax.swing.JLabel();
        txtEstabFax = new javax.swing.JFormattedTextField();
        jLabel10 = new javax.swing.JLabel();
        txtEstabTelefone = new javax.swing.JFormattedTextField();
        jLabel28 = new javax.swing.JLabel();
        txtEstabBairro = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        txtRepresNome = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        txtRepresRg = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        txtRepresSsp = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        txtRepresDateExp = new javax.swing.JFormattedTextField();
        txtRepresCpf = new javax.swing.JFormattedTextField();
        txtRepresNome1 = new javax.swing.JTextField();
        btnSalvar = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnNovo = new javax.swing.JButton();
        btnDeletar = new javax.swing.JButton();
        btnDeletar1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastro de Estabelecimentos");
        setLocationByPlatform(true);
        setResizable(false);

        jtpEstabelecimentos.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        dgvEstabelecimentos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        dgvEstabelecimentos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(dgvEstabelecimentos);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/forms/resources/search_32x32.png"))); // NOI18N
        jButton1.setToolTipText("Pesquisar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Pesquisa:");

        buttonGroup1.add(rbtNome);
        rbtNome.setSelected(true);
        rbtNome.setText("NOME \\ RAZÃO SOCIAL");

        buttonGroup1.add(rbdCnpj);
        rbdCnpj.setText("CNPJ / CPF:");

        javax.swing.GroupLayout jpnPesquisaLayout = new javax.swing.GroupLayout(jpnPesquisa);
        jpnPesquisa.setLayout(jpnPesquisaLayout);
        jpnPesquisaLayout.setHorizontalGroup(
            jpnPesquisaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnPesquisaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtPesquisa, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(rbtNome)
                .addGap(18, 18, 18)
                .addComponent(rbdCnpj)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(jpnPesquisaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jpnPesquisaLayout.createSequentialGroup()
                    .addGap(7, 7, 7)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 692, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        jpnPesquisaLayout.setVerticalGroup(
            jpnPesquisaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnPesquisaLayout.createSequentialGroup()
                .addGroup(jpnPesquisaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jpnPesquisaLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jpnPesquisaLayout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addGroup(jpnPesquisaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txtPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(rbdCnpj)
                            .addComponent(rbtNome))))
                .addContainerGap(344, Short.MAX_VALUE))
            .addGroup(jpnPesquisaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpnPesquisaLayout.createSequentialGroup()
                    .addContainerGap(120, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jtpEstabelecimentos.addTab("Pesquisa", jpnPesquisa);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED), "DADOS DO ESTABELECIMENTO", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 12), new java.awt.Color(0, 0, 0))); // NOI18N

        jLabel2.setText("* RAZÃO SOCIAL / PESSOA  FISICA :");

        txtEstabNome.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));

        jLabel3.setText("ATIVIDADE:");

        jLabel4.setText("NOME FANTASIA:");

        jLabel5.setText("* CNPJ / CPF:");

        jLabel6.setText("* ENDEREÇO:");

        jLabel7.setText("MUNICIPIO:");

        txtEstabMunicipio.setText("Rio Quente");

        jLabel8.setText("CEP: ");

        try {
            txtEstabCep.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##.###-###")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txtEstabCep.setText("75695000");

        jLabel9.setText("FONE: ");

        try {
            txtEstabFax.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##-####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txtEstabFax.setText("64");

        jLabel10.setText("FAX:");

        try {
            txtEstabTelefone.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##-####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txtEstabTelefone.setText("64");

        jLabel28.setText("* BAIRRO:");

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED), "DADOS DO REPRESENTANTE LEGAL", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 12), new java.awt.Color(0, 0, 0))); // NOI18N

        jLabel11.setText("* NOME:");

        txtRepresNome.setColumns(17);
        txtRepresNome.setToolTipText("Máximo de 17 caracteres");

        jLabel12.setText("* CPF:");

        jLabel13.setText("* RG:");

        jLabel14.setText("* SSP:");

        jLabel15.setText("* DT DE EXPEDIÇÃO:");

        try {
            txtRepresDateExp.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        try {
            txtRepresCpf.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel13)
                        .addGap(18, 18, 18)
                        .addComponent(txtRepresRg, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel14)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtRepresSsp, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel15)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtRepresDateExp, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11)
                            .addComponent(jLabel12))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel4Layout.createSequentialGroup()
                                .addComponent(txtRepresNome, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtRepresNome1, javax.swing.GroupLayout.PREFERRED_SIZE, 336, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(txtRepresCpf, javax.swing.GroupLayout.DEFAULT_SIZE, 576, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(txtRepresNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtRepresNome1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel12)
                    .addComponent(txtRepresCpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(txtRepresRg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtRepresDateExp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel14)
                    .addComponent(txtRepresSsp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel15))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtEstabEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, 259, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtEstabMunicipio, javax.swing.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtEstabCpfCnpj, javax.swing.GroupLayout.DEFAULT_SIZE, 572, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtEstabNFantasia, javax.swing.GroupLayout.DEFAULT_SIZE, 545, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtEstabAtividade, javax.swing.GroupLayout.DEFAULT_SIZE, 586, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtEstabNome, javax.swing.GroupLayout.DEFAULT_SIZE, 432, Short.MAX_VALUE))))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel28)
                        .addGap(18, 18, 18)
                        .addComponent(txtEstabBairro, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel9)
                        .addGap(2, 2, 2)
                        .addComponent(txtEstabTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtEstabFax, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtEstabCep, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE))
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtEstabNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtEstabAtividade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtEstabNFantasia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtEstabCpfCnpj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtEstabEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(txtEstabMunicipio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel28)
                    .addComponent(txtEstabBairro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(txtEstabTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)
                    .addComponent(txtEstabFax, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(txtEstabCep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout jpnCadastroLayout = new javax.swing.GroupLayout(jpnCadastro);
        jpnCadastro.setLayout(jpnCadastroLayout);
        jpnCadastroLayout.setHorizontalGroup(
            jpnCadastroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jpnCadastroLayout.setVerticalGroup(
            jpnCadastroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnCadastroLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jtpEstabelecimentos.addTab("Principal", jpnCadastro);

        btnSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/forms/resources/save_32x32.png"))); // NOI18N
        btnSalvar.setToolTipText("Salvar");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/forms/resources/edit_32x32.png"))); // NOI18N
        btnEditar.setToolTipText("Editar");
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });

        btnNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/forms/resources/new_32x32.png"))); // NOI18N
        btnNovo.setToolTipText("Novo");
        btnNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNovoActionPerformed(evt);
            }
        });

        btnDeletar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/forms/resources/delete_32x32.png"))); // NOI18N
        btnDeletar.setToolTipText("Deletar");
        btnDeletar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeletarActionPerformed(evt);
            }
        });

        btnDeletar1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/forms/resources/exit_32x32.png"))); // NOI18N
        btnDeletar1.setToolTipText("Sair");
        btnDeletar1.setName("btnExit"); // NOI18N
        btnDeletar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeletar1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jtpEstabelecimentos, javax.swing.GroupLayout.DEFAULT_SIZE, 719, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(318, Short.MAX_VALUE)
                .addComponent(btnNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnDeletar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnDeletar1, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnDeletar, btnDeletar1, btnEditar, btnNovo, btnSalvar});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jtpEstabelecimentos, javax.swing.GroupLayout.PREFERRED_SIZE, 439, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnDeletar1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, Short.MAX_VALUE)
                    .addComponent(btnDeletar, javax.swing.GroupLayout.PREFERRED_SIZE, 34, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 41, Short.MAX_VALUE)
                        .addComponent(btnNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 41, Short.MAX_VALUE)
                        .addComponent(btnSalvar)))
                .addGap(25, 25, 25))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-719)/2, (screenSize.height-527)/2, 719, 527);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            // TODO add your handling code here:
            String sql;
            if(txtPesquisa.toString().equals(""))
            {
                JOptionPane.showMessageDialog(null, "Preencha o campo pesquisa");
                return;
            }
                
            if (rbtNome.isSelected()) {
                sql = "estab_name LIKE '" + txtPesquisa.getText()+"%'";
            } else {
                sql = "estab_cnpjCpf LIKE '" + txtPesquisa.getText()+"%'";
            }

            if (!listEstab.fillList(sql)) {
                JOptionPane.showMessageDialog(null, "Erro "+ Database.ErrorMessage);
                return;
            }
            fillGridView();
            btnEditar.setEnabled(true);
            btnDeletar.setEnabled(true);
        } catch (SQLException ex) {
            Logger.getLogger(frmRegisterEstabelecimento.class.getName()).log(Level.SEVERE, null, ex);
        }



}//GEN-LAST:event_jButton1ActionPerformed

    private void btnNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNovoActionPerformed
        clearForm();
        jtpEstabelecimentos.setSelectedIndex(1);
        txtEstabNome.requestFocus();
        status = true;
        btnEditar.setEnabled(false);
        btnDeletar.setEnabled(false);
        estab.clear();
    }//GEN-LAST:event_btnNovoActionPerformed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        try {
            editar();
        } catch (SQLException ex) {
            Logger.getLogger(frmRegisterEstabelecimento.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnEditarActionPerformed

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
        if(txtEstabNome.getText().equals("") || txtEstabCpfCnpj.getText().equals("") || txtEstabEndereco.getText().equals(""))
        {
            JOptionPane.showMessageDialog(null, "Peencha os campos obrigatorios *");
            return;
        }
        if(txtEstabBairro.getText().equals("") || txtRepresNome.getText().equals("") || txtRepresCpf.getText().equals(""))
        {
            JOptionPane.showMessageDialog(null, "Peencha os campos obrigatorios *");
            return;
        }

        if(txtRepresCpf.getText().equals("") || txtRepresSsp.getText().equals("") || txtRepresDateExp.getText().equals(""))
        {
            JOptionPane.showMessageDialog(null, "Peencha os campos obrigatorios *");
            return;
        }

        salvar();
    }//GEN-LAST:event_btnSalvarActionPerformed

    private void btnDeletarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeletarActionPerformed
        try {
            deletar();
        } catch (SQLException ex) {
            Logger.getLogger(frmRegisterEstabelecimento.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnDeletarActionPerformed

    private void btnDeletar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeletar1ActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnDeletar1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                //new frmRegisterEstabelecimento().setVisible(true);
            }
        });
    }


    //Methods
    private void fillGridView() throws SQLException
    {
        String[] colunas = new String[]{"Codigo","Nome","CPF"};
        String[][] dados = new String[][]{{}};
        dtmEstab = new DefaultTableModel(dados, colunas);

        String estabelecimento[] = new String[3];
        int aux = 0;
        for(int i = 0; i < listEstab.count(); i++)
        {
            estabelecimento[0] = listEstab.get(i).getId();
            estabelecimento[1] = listEstab.get(i).getEstab_name();
            estabelecimento[2] = listEstab.get(i).getEstab_cnpjCpf();
            dtmEstab.addRow(estabelecimento);
            if (i == 0){
               dtmEstab.removeRow(0);
            }
        }
        dgvEstabelecimentos.setModel(dtmEstab);
    }

    private void salvar()
    {
        estab.setEstab_name(txtEstabNome.getText());
        estab.setFantasyName(txtEstabNFantasia.getText());
        estab.setEstab_atividade(txtEstabAtividade.getText());
        estab.setEstab_cnpjCpf(txtEstabCpfCnpj.getText());
        estab.setEstab_endereco(txtEstabEndereco.getText());
        estab.setEstab_bairro(txtEstabBairro.getText());
        estab.setEstab_municipio(txtEstabMunicipio.getText());
        estab.setEstab_telefone(txtEstabTelefone.getText());
        estab.setEstab_cep(txtEstabCep.getText());
        estab.setEstab_fax(txtEstabFax.getText());
        estab.setRepres_name(txtRepresNome.getText());
        estab.setRepres_sobrenome(txtRepresNome1.getText());
        estab.setRepres_cpf(txtRepresCpf.getText());
        estab.setRepres_rg(txtRepresRg.getText());
        estab.setRepres_ssp(txtRepresSsp.getText());
        estab.setRepres_dataExpedicao(txtRepresDateExp.getText());
        estab.setEnable(true);
        
        String mensagem = "";
        if(status){
            if(estab.save())
               mensagem = "Registro salvo com sucesso";
            else
               mensagem = "Erro ao tentar gravar o registro";
         }
         else{
            if(estab.update())
               mensagem = "Registro salvo com sucesso";
            else
               mensagem = "Erro ao tentar gravar o registro";
         }
         JOptionPane.showMessageDialog(null,mensagem);
         status = true;
         jtpEstabelecimentos.setSelectedIndex(0);
    }

    private void editar() throws SQLException
    {
         estab.setId(idGredViewSelectedRow());
         if(!estab.get())
         {
             JOptionPane.showMessageDialog(null, "Erro:"+ estab.getErrorMessage());
             return;
         }
         clearForm();
         fillEdits();
         status = false;
         limpaDVG();
         btnEditar.setEnabled(false);
         jtpEstabelecimentos.setSelectedIndex(1);
    }

    private boolean deletar() throws SQLException
    {
        int option = JOptionPane.showConfirmDialog(null, "Deseja realmente excluir este registro ?");
        if(option == 0)
        {
            estab.setId(idGredViewSelectedRow());
            estab.get();
            estab.setEnable(false);
            if(!estab.update())
                JOptionPane.showMessageDialog(null, "Erro ao tentar deletar o registro");
            listEstab.fillList();
            fillGridView();
        }
        return true;
    }

    public void clearForm()
    {
        txtEstabNome.setText("");
        txtEstabNFantasia.setText("");
        txtEstabAtividade.setText("");
        txtEstabCpfCnpj.setText("");
        txtEstabEndereco.setText("");
        txtEstabBairro.setText("");
        txtEstabMunicipio.setText("RIO QUENTE");
        txtEstabTelefone.setText("64");
        txtEstabCep.setText("75695000");
        txtEstabFax.setText("64");
        txtRepresNome.setText("");
        txtRepresNome1.setText("");
        txtRepresCpf.setText("");
        txtRepresRg.setText("");
        txtRepresSsp.setText("");
        txtRepresDateExp.setText("");
        txtPesquisa.setText("");

        rbtNome.setSelected(true);
    }

    public void fillEdits()
    {
        txtEstabNome.setText(estab.getEstab_name());
        txtEstabNFantasia.setText(estab.getFantasyName());
        txtEstabAtividade.setText(estab.getEstab_atividade());
        txtEstabCpfCnpj.setText(estab.getEstab_cnpjCpf());
        txtEstabEndereco.setText(estab.getEstab_endereco());
        txtEstabBairro.setText(estab.getEstab_bairro());
        txtEstabMunicipio.setText(estab.getEstab_municipio());
        txtEstabTelefone.setText(estab.getEstab_telefone());
        txtEstabCep.setText(estab.getEstab_cep());
        txtEstabFax.setText(estab.getEstab_fax());
        txtRepresNome.setText(estab.getRepres_name());
        txtRepresNome1.setText(estab.getRepres_sobrenome());
        txtRepresCpf.setText(estab.getRepres_cpf());
        txtRepresRg.setText(estab.getRepres_rg());
        txtRepresSsp.setText(estab.getRepres_ssp());
        txtRepresDateExp.setText(estab.getRepres_dataExpedicao());
    }

    private String idGredViewSelectedRow()
    {
        int  index = dgvEstabelecimentos.getSelectedRow();
        String id = dgvEstabelecimentos.getValueAt(index,0).toString();
        return id;
    }

    private void limpaDVG()
    {
        String[] colunas = new String[]{"Codigo","Estabelecimento","Ano","Tecnico"};
        String[][] dados = new String[][]{{}};
        dtmEstab = new DefaultTableModel(dados, colunas);
        dgvEstabelecimentos.setModel(dtmEstab);
    }

    //End Methods

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDeletar;
    private javax.swing.JButton btnDeletar1;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnNovo;
    private javax.swing.JButton btnSalvar;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JTable dgvEstabelecimentos;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel28;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPanel jpnCadastro;
    private javax.swing.JPanel jpnPesquisa;
    private javax.swing.JTabbedPane jtpEstabelecimentos;
    private javax.swing.JRadioButton rbdCnpj;
    private javax.swing.JRadioButton rbtNome;
    private javax.swing.JTextField txtEstabAtividade;
    private javax.swing.JTextField txtEstabBairro;
    private javax.swing.JFormattedTextField txtEstabCep;
    private javax.swing.JTextField txtEstabCpfCnpj;
    private javax.swing.JTextField txtEstabEndereco;
    private javax.swing.JFormattedTextField txtEstabFax;
    private javax.swing.JTextField txtEstabMunicipio;
    private javax.swing.JTextField txtEstabNFantasia;
    private javax.swing.JTextField txtEstabNome;
    private javax.swing.JFormattedTextField txtEstabTelefone;
    private javax.swing.JTextField txtPesquisa;
    private javax.swing.JFormattedTextField txtRepresCpf;
    private javax.swing.JFormattedTextField txtRepresDateExp;
    private javax.swing.JTextField txtRepresNome;
    private javax.swing.JTextField txtRepresNome1;
    private javax.swing.JTextField txtRepresRg;
    private javax.swing.JTextField txtRepresSsp;
    // End of variables declaration//GEN-END:variables
}
